package com.example.demo.hutool.redis.pipeline;

import com.example.demo.hutool.redis.RedisTypeTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

public class PipelineTest {


    public static void main(String[] args) throws InterruptedException {
        //----------------------
        // 总数1000  每条插入总耗时5537毫秒  批量插入耗时63毫秒/每条插入耗时17010毫秒 批量插入耗时46毫秒/每条插入耗时13144毫秒 批量插入耗时54毫秒
        // 总数10000  每条插入总耗时77104毫秒  批量插入耗时400毫秒/每条插入耗时132852毫秒批量插入耗时143毫秒/每条插入耗时67562毫秒批量插入耗时135毫秒
        // 总数100  每条插入总耗时1375毫秒  批量插入耗时41毫秒/每条插入耗时1174毫秒 批量插入耗时40毫秒/每条插入耗时774毫秒 批量插入耗时36毫秒
//        jedisEvery();
        //  jedisPipeline();
        //----------------------
        // 总数100  每条查询总耗时1127毫秒  批量查询耗时37毫秒/每条查询耗时468毫秒 批量查询耗时33毫秒/每条查询耗时1205毫秒 批量查询耗时46毫秒
        // 总数500  每条查询总耗时2744毫秒  批量查询耗时43毫秒/每条查询耗时4817毫秒 批量查询耗时61毫秒/每条查询耗时2851毫秒 批量查询耗时45毫秒
        // 总数1000  每条查询耗时12642毫秒  批量查询耗时66毫秒/每条查询耗时8188毫秒 批量查询耗时54毫秒/每条查询耗时7393毫秒 批量查询耗时42毫秒
        // 总数5000  每条查询耗时75275毫秒  批量查询耗时86毫秒/每条查询耗时47776毫秒 批量查询耗时96毫秒/每条查询耗时55150毫秒 批量查询耗时84毫秒
//        jedisEveryQuery();
//        jedisPipelineQuery();

        //----------------------
//        jedisPipeline();
        jedisPipeline2();

//        int size = 10000;
//        JedisPool jedisPool = RedisTypeTest.getJedisPool();
//        Jedis jedis = jedisPool.getResource();
//        long begin = System.currentTimeMillis();
//        for (int i = 0; i < size; i++) {
//            String s = i + "pipeline";
//            jedis.get(s);
//        }
//        long end = System.currentTimeMillis();
//        System.out.println("mget批量查询耗时" + (end - begin) + "毫秒");
    }

    public static void jedisMgetQuery() {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        int size = 400000;
        String[] objects = new String[size];
        Jedis jedis = jedisPool.getResource();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            String s = i + "pipeline";
            objects[i] = s;
        }
        List<String> mget = jedis.mget(objects);
        long end = System.currentTimeMillis();
        System.out.println("mget批量查询耗时" + (end - begin) + "毫秒");
    }

    public static void jedisEvery() {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        Jedis jedis = jedisPool.getResource();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            jedis.set(i + "every", i + "");
        }
        long end = System.currentTimeMillis();
        System.out.println("每条插入耗时" + (end - begin) + "毫秒");
        jedis.close();
        jedisPool.close();
    }

    public static void jedisEveryQuery() {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        Jedis jedis = jedisPool.getResource();
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            String s = jedis.get(i + "sxm");
        }
        long end = System.currentTimeMillis();
        System.out.println("每条查询耗时" + (end - begin) + "毫秒");
        jedis.close();
        jedisPool.close();
    }

    public static void jedisPipelineQuery() {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        Jedis jedis = jedisPool.getResource();
        long begin = System.currentTimeMillis();
        //开启redis管道
        Pipeline pipeline = jedis.pipelined();
        int size = 10000;
        for (int i = 0; i < 10; i++) {
            String s = i + "sxm";
            pipeline.get(s);
        }
        List<Object> objects = pipeline.syncAndReturnAll();
        //  List o = (List) objects.get(0);
        long end = System.currentTimeMillis();
        System.out.println("批量查询耗时" + (end - begin) + "毫秒");
        jedis.close();
        jedisPool.close();
    }

    public static void jedisPipeline() {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        Jedis jedis = jedisPool.getResource();
        long begin = System.currentTimeMillis();
        //开启redis管道
        Pipeline pipeline = jedis.pipelined();
//        for (int i = 0; i < 400000; i++) {
//            pipeline.sadd(i + "Pipelineset", i + "");
//        }
//        pipeline.sync();
//        for (int i = 0; i < 400000; i++) {
//            pipeline.zadd(i + "Pipelinezset", i, i + "");
//        }
        //       pipeline.sync();
        for (int i = 0; i < 10000; i++) {
            pipeline.set(i + "Pipeline", i + "");
        }
        pipeline.sync();
//        for (int i = 0; i < 400000; i++) {
//            pipeline.lpush(i + "Pipelinelist", i + "");
//
//        }
//        pipeline.sync();
//        for (int i = 0; i < 400000; i++) {
//            pipeline.hset(i + "sxmhash", i + "", i + "");
//        }
//        pipeline.sync();

        //  pipeline.mset(strings);
        //管道提交数据
        long end = System.currentTimeMillis();
        System.out.println("批量插入耗时" + (end - begin) + "毫秒");
        jedis.close();
        jedisPool.close();
    }

    public static void jedisPipeline2() throws InterruptedException {
        JedisPool jedisPool = RedisTypeTest.getJedisPool();
        Jedis jedis = jedisPool.getResource();
    //    jedis.flushDB();
        long begin = System.currentTimeMillis();
        //开启redis管道
        Pipeline pipeline = jedis.pipelined();
        String[] strings = {"1","2"};

//        for (int k = 0; k < 1000000000; k++) {
//            for (int i = 0; i < 50000; i++) {
//                pipeline.set("string"+i, i + "u");
////            jedis.set("string"+ UUID.randomUUID().toString(), i + "u");
//            }
//            pipeline.sync();
//        }
            for (int i = 0; i < 800000; i++) {
                pipeline.set("string"+i, i + "4");
//            jedis.set("string"+ UUID.randomUUID().toString(), i + "u");
            }
            pipeline.sync();

        for (int i = 0; i < 800000; i++) {
            pipeline.sadd(i + "Pipelineset", "4");
        }
        pipeline.sync();
//
////
        for (int i = 0; i < 800000; i++) {
            pipeline.zadd(i + "Pipelinezset", i, i + "4");
        }
        pipeline.sync();

//
//
//
        for (int i = 0; i < 800000; i++) {
            pipeline.lpush(i + "Pipelinelist", i + "4");

        }
        pipeline.sync();
//
//
        for (int i = 0; i < 800000; i++) {
            pipeline.hset(i + "sxmhash", i + "", i + "4");
        }
        pipeline.sync();

//        for (int k = 0; k < 1000000; k++) {
//            for (int i = 0; i < 400000; i++) {
//                pipeline.set(i + "Pipeline"+ UUID.randomUUID().toString(), i + "");
//            }
//            pipeline.sync();
//
//        }

        //  pipeline.mset(strings);
        //管道提交数据
        long end = System.currentTimeMillis();
        System.out.println("批量插入耗时" + (end - begin) + "毫秒");
        jedis.close();
        jedisPool.close();
    }
}
